﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Logic.Common;
using Job.Entity;

namespace Job.Web.userControls.Admin
{
    public partial class CommonDictionaryInfo : BaseCtrl
    {
        DictionaryLogic logic = new DictionaryLogic();

        private int id
        {
            get {return myR["id"].IntValue;}
        }
        private int dictClass
        {
            get { return myR["dictclass"].IntValue; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!LoadData())
                {
                    tbName.Enabled = false;
                    rblStatus.Enabled = false;
                    btSave.Enabled = false;
                }
            }
        }

        private bool LoadData()
        {
            if (id > 0)
            {
                try
                {
                    JobCommonDictionary dict = logic.SelectCommon(id);
                    if (dict == null) return false;

                    this.tbName.Text = dict.DictName;
                    if (dict.DictState.Value)
                        this.rblStatus.Items[0].Selected = true;
                    else
                        this.rblStatus.Items[1].Selected = true;
                    this.hidId.Value = dict.DictId.ToString();
                    this.hidClass.Value = dict.DictClass;

                    return true;
                }
                catch(Exception ex)
                {
                    Logger.WriteLog(ex.ToString());
                    return false;
                }

            }
            else
            {
                hidClass.Value = Constants.Num2FixLenStr(dictClass, Constants.DICT_CLASS_LEN);
                hidId.Value = "0";
                tbName.Text = "";
                rblStatus.Items[0].Selected = true;
                return true;
            }

        }

        protected void btReturn_Click(object sender, EventArgs e)
        {
            string url = "AdminMain.aspx?controlType=" + dictClass.ToString() + "&dictclass=" + dictClass.ToString();
            Response.Redirect(url);
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            JobCommonDictionary dict = new JobCommonDictionary();
            dict.DictId = Convert.ToInt32(hidId.Value);
            dict.DictClass = hidClass.Value;
            dict.DictName = tbName.Text.Trim();
            if (rblStatus.Items[0].Selected)
                dict.DictState = true;
            else
                dict.DictState = false;

            if (dict.DictId == 0)
                logic.InsertCommon(dict);
            else
                logic.UpdateCommon(dict);

            MemoryDictionary.LoadCommonDictionary();

            string message = Server.UrlEncode(string.Format("保存配置项成功！"));
            string urlPath = Server.UrlEncode("/AdminWeb/AdminMain.aspx?controlType=" 
                + dictClass.ToString() + "&dictclass=" + dictClass.ToString());
            string url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
            Response.Redirect(url);

        }
    }
}